package com.specter.dev.code.execute;

import static com.specter.utils.StringUtils.cap_first;
import static com.specter.utils.StringUtils.toJavaName;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.specter.dev.code.builder.GenConfiguration.ModuleInput;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * Note:通过读取Eclipse插件完成的配置表格并独立生成一个HTML文件，以方便阅读<br/>
 * <br/>
 * 
 * <b> &lt;input type="ixml" src="/home/table.html" tgt="a,b"
 * fix="-IDS_%s+_ENTITY" /&gt; </b>
 * 
 * @author Liang.Wang
 * @version Mar 23, 2017
 */
public class ModuleInputExe4Workbook implements ModuleInputExe {
	private static Workbook wb;

	@Override
	public List<?> input(ModuleInput input) {
		List<ModuleInputExe.Entity> list = new ArrayList<>();
		wb = excel(input.getSrc());
		if (wb == null) {
			return list;
		}

		int i = 0;
		Sheet sheet = null;
		while ((sheet = wb.getSheetAt(i)) != null) {
			Row rows = sheet.getRow(0);
			//excel按照指定格式和sheet名称
			if((rows == null || rows.toString().isEmpty()) || sheet.getSheetName().startsWith("Sheet")) {
				break;
			}
			String[] name = sheet.getSheetName().split("-");

			ModuleInputExe.Entity entity = new ModuleInputExe.Entity();
			entity.setTableName(name[0]);
			entity.setTableRemark(name[1]);
			entity.setEntityName(cap_first(name[0]));
			List<EntityProperty> propertie = new ArrayList<>();
			for (int e = sheet.getFirstRowNum() + 1; e <= sheet.getLastRowNum(); e++) {
				ModuleInputExe.EntityProperty property = new ModuleInputExe.EntityProperty();
				Row row = sheet.getRow(e);
				String isId = row.getCell(4).toString();
				property.setColumn(row.getCell(0).toString());
				property.setType(row.getCell(1).toString());
				property.setRemark(row.getCell(3).toString());
				property.setName(toJavaName(property.getColumn()));
				property.setClazz(this.clazz(property.getType()));
				if ("y".equals(isId)) {
					entity.setId(property);
				} else {
					propertie.add(property);
					entity.setProperties(propertie);
				}
			}
			i++;
			list.add(entity);
		}
		return list;
	}

	public static Workbook excel(String file) {
		try {
			File excel = new File(file);
			if (excel.isFile() && excel.exists()) {
				String[] split = excel.getName().split("\\.");
				if ("xls".equals(split[1])) {
					FileInputStream fis = new FileInputStream(excel); // 文件流对象
					wb = new HSSFWorkbook(fis);
				} else if ("xlsx".equals(split[1])) {
					wb = new XSSFWorkbook(excel);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return wb;
	}

	private Map<Object, String> types = new HashMap<Object, String>() {
		private static final long serialVersionUID = 1L;

		{
			put("varchar", "String");
			put("int", "Integer");
			put("date", "java.util.Date");
		}
	};

	private String clazz(Object typeKey) {
		String type = types.get(typeKey);
		return type == null ? "String" : type;
	}

}
